/*
 * @Autor: dc
 * @Date: 2022-03-30 17:34:24
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-18 16:03:19
 * @Description: 
 */

import store from '../store.js'

const initState = {
    userName: '张德帥',//用户名称
    // 当前树的阶段
    phase: 1,
    // 用户能量值
    energyNum: 0,//当前能量值
    energyFull: 100,//满值
    energyTimeNum: 10,// 每次添加的能量值
    // 树的所有阶段图片样式
    treeArr: [
        {
            icon: require('../../assets/tree/tree1.png'),
            id: 1,//阶段id
            energyFull: 20,//该阶段值 超过这个阶段将进行下个阶段
        },
        {
            icon: require('../../assets/tree/tree2.png'),
            id: 2,
            energyFull: 40
        },
        {
            icon: require('../../assets/tree/tree3.png'),
            id: 3,
            energyFull: 60
        },
        {
            icon: require('../../assets/tree/tree4.png'),
            id: 4,
            energyFull: 80
        },
        {
            icon: require('../../assets/tree/tree5.png'),
            id: 5,
            energyFull: 100
        },
    ],
    // 树
    isPlanting: true,//树是否可以动画   


    // 操作栏列表
    operationArr: [
        {
            icon: require('../../assets/operation/operation_icon1.png'),//图标
            name: '浇水',//名字
            prepare: 0,//操作时间
            id: 1,
            cooling: 60,//冷却时间
        },
        {
            icon: require('../../assets/operation/operation_icon2.png'),
            name: '施肥',
            prepare: 0,
            id: 2,
            cooling: 60
        },
        {
            icon: require('../../assets/operation/operation_icon3.png'),
            name: '松土',
            prepare: 0,
            id: 3,
            cooling: 60
        },
        {
            icon: require('../../assets/operation/operation_icon4.png'),
            name: '阳光',
            prepare: 0,
            id: 4,
            cooling: 60
        }
    ],
    isOperation: true,//是否可以操作
} //初始化状态


function countReducer(preState = initState, action) {
    // 从action对象中获取:type\data
    const { type, data } = action;
    // 根据type决定如何加工数据
    switch (type) {
        // 加能量
        case 'incrementEnergyNum':
            preState.energyNum += data * 1
            preState = conversionLevel(preState)
            StoreDataFun(preState)
            return preState
        // 是否在加能量
        case 'plantingBoolean':
            preState.isPlanting = data;
            // 恢复操作
            preState.isOperation = true;
            return preState
        // 操作栏状态
        case 'actionBarConfig':
            preState.operationArr = preState.operationArr.map(item => {
                if (item.name === data.name) item.prepare = data.prepare
                return item
            })
            return preState

        // 是否可以操作
        case 'operationBoolean':
            preState.isOperation = data
            return preState
        // 初始化参数
        case 'Initialize':
            let storeState = window.localStorage.getItem('treeInitState');
            if (storeState) {
                preState.energyNum = storeState * 1;
                preState = conversionLevel(preState)
            }
            return preState
        // 存储用户数据
        case 'StoreData':
            StoreDataFun(preState)
            return preState
        // 设置用户名称
        case 'setUpUserNmae':
            preState.userName = data;
            return preState
        default:
            return preState;
    }
}
// 存储用户数据
const StoreDataFun = (data) => {
    window.localStorage.setItem('treeInitState', data.energyNum)
}
// 换算树等级
const conversionLevel = (data) => {
    let num = data.treeArr.filter(item => data.energyNum < item.energyFull)
    data.phase = num.length > 0 ? num[0].id : data.treeArr[data.treeArr.length - 1].id;
    return data
}

// 暴露count_reducer
export default countReducer
